/**
 * 项目结构管理器
 */
define(function(require, exports, module) {
    /**
     * 结构上的点
     */
    var nodes = [];

    /**
     * 结构上的连线
     */
    var edges = [];

    /**
     * 节点map 去重使用 同时记录引用次数
     */
    var nodeMap = {};
    
    /**
     * 连线次数map 用于记录连接次数 同时用于ID生成 防止重复ID
     */
    var edgeConnectTimesMap = {};

    /**
     * 转换项目结构数据为绘制器所需要的数据结构
     * @param {Object} structData
     */
    function converStructData(structData) {
        for(var key in structData) {
            prepareNodeDatas(key, structData[key]);
            prepareEdges(key, structData[key]);
        }
    }

    /**
     * 依据节点名称准备绘制器所需要的节点数据
     * @param {Object} componentName
     */
    function prepareNodeDatas(componentName, requireComponentNames) {
        prepareNodeData(componentName);
        for(var i = 0, length = requireComponentNames.length; i < length; i++) {
            prepareNodeData(requireComponentNames[i]);
        }
    }
    
    
    function prepareNodeData(nodeName) {
        if(!checkNodeIsExist(nodeName)) {
            nodes.push({
                id: nodeName
            });
            nodeMap[nodeName] = 0;
        }
        
    }

    function checkNodeIsExist(nodeName) {
        if(typeof nodeMap[nodeName] !== "number") {
            return false;
        }else{
            return true;
        }
    }

    function prepareEdges(sourceName, targetNames) {
        for(var i = 0, length = targetNames.length; i < length; i++) {
            prepareEdge(sourceName, targetNames[i]);
        }
    }
    
    function prepareEdge(sourceName, targetName) {
        if(!edgeConnectTimesMap[sourceName + "->" +targetName]) {
            edgeConnectTimesMap[sourceName + "->" +targetName] = 1;
        } else {
            edgeConnectTimesMap[sourceName + "->" +targetName] ++;
        }
        
        edges.push({
            id: sourceName + "->" +targetName + ":" + edgeConnectTimesMap[sourceName + "->" +targetName],
            source: sourceName,
            target: targetName
        });
        
        nodeMap[targetName] ++;
    }

    return {
        nodes: nodes,
        edges: edges,
        nodeMap : nodeMap,
        edgeConnectTimesMap : edgeConnectTimesMap,
        converStructData: converStructData
    }
})